<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="首选日期" prop="preferredDate">
      <el-input v-model="dataForm.preferredDate" placeholder="首选日期"></el-input>
    </el-form-item>
    <el-form-item label="客户姓名" prop="custName">
      <el-input v-model="dataForm.custName" placeholder="客户姓名"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          csltnId: 0,
          createTime: '',
          userId: '',
          preferredDate: '',
          custName: '',
          email: '',
          phone: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          preferredDate: [
            { required: true, message: 'mlsId不能为空', trigger: 'blur' }
          ],
          custName: [
            { required: true, message: '请求参数内容不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '收藏状态不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.csltnId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.csltnId) {
            this.$http({
              url: this.$http.adornUrl(`/crm/consultation/info/${this.dataForm.csltnId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.consultation.createTime
                this.dataForm.userId = data.consultation.userId
                this.dataForm.preferredDate = data.consultation.preferredDate
                this.dataForm.custName = data.consultation.custName
                this.dataForm.email = data.consultation.email
                this.dataForm.phone = data.consultation.phone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        var _this = this
        _this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            _this.$http({
              url: _this.$http.adornUrl(`/crm/consultation/${!_this.dataForm.csltnId ? 'save' : 'update'}`),
              method: 'post',
              data: _this.$http.adornData({
                'csltnId': _this.dataForm.csltnId || undefined,
                'createTime': _this.dataForm.createTime,
                'userId': _this.dataForm.userId,
                'preferredDate': _this.dataForm.preferredDate,
                'custName': _this.dataForm.custName,
                'email': _this.dataForm.email,
                'phone': _this.dataForm.phone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                _this.$message.success('success', 1, function () {
                  _this.visible = false
                  _this.$emit('refreshDataList')
                })

              } else {
                _this.$message.error(data.msg, 1)
              }
            })
          }
        })
      }
    }
  }
</script>
